package com.lynn.salary.util;

import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResultAnalysis {

	public List<Map<String, Object>> analysis(ResultSet r) {
		ResultSetMetaData rsmd = null;
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		try {
			rsmd = r.getMetaData();
			System.out.print("= = == == == == == == == == == == == == == == == == == == == == == == == ===");
			System.out.println(" == == == == == == == == == == == ==");
			while (r.next()) {
				Map<String, Object> record = new HashMap<String, Object>();
				for (int i = 1; i <= rsmd.getColumnCount(); i++) {
					String column = rsmd.getColumnLabel(i);
					Object value = r.getObject(i);
					// String type = getType(rsmd.getColumnType(i));
					ToolMethod.print(column, ""+value);
					if (record.containsKey(column))
						System.err.println("警告：出现重复字段[" + column + "]值将被覆盖,请使用别名");
					record.put(column, value);
				}
				System.out.print("= == == == == == == == == == == == == == == == == == == == == == == == == ==");
				System.out.println(" == == == == == == == == == == == ==");
				result.add(record);
			}
		} catch (Exception e) {
			System.err.println("ResultAnalysis::analysis error");
			return null;
		}

		return result;
	}

	public <T> List<T> analysis(List<Map<String, Object>> r2, Class<T> c) {
		List<T> result = new ArrayList<T>();
		if (r2 == null)
			return null;

		for (Map<String, Object> tmp : r2) {
			T t;
			try {
				t = c.newInstance();
			} catch (Exception e1) {
				System.err.println("ResultAnalysis::analysis error");
				return null;
			}
			for (String key : tmp.keySet()) {
				try {
					Method getMethod = c.getMethod(ToolMethod.change2GetMethodName(key));
					Method setMethod = c.getMethod(ToolMethod.change2SetMethodName(key), getMethod.getReturnType());
//					System.out.println(setMethod.getName() + ">>" + tmp.get(key));
//					ToolMethod.print(""+setMethod.getName(), ""+tmp.get(key));
					setMethod.invoke(t, tmp.get(key));
				} catch (Exception e) {
					e.printStackTrace();
					System.err.println("ResultAnalysis::analysis error");
					return null;
				}
			}
			result.add(t);
		}
		return result;
	}

	// private String getType(int sqlType) {
	//
	// switch (sqlType) {
	// case Types.BIT:
	// return ("java.lang.Boolean");
	// case Types.SMALLINT:
	// return ("java.lang.Integer");
	// case Types.INTEGER:
	// return ("java.lang.Integer");
	// case Types.BIGINT:
	// return ("java.lang.Long");
	// case Types.NUMERIC:
	// return ("java.math.BigDecimal");
	// case Types.REAL:
	// return ("java.lang.Float");
	// case Types.DOUBLE:
	// return ("java.lang.Double");
	// case Types.CHAR:
	// case Types.VARCHAR:
	// return ("java.lang.String");
	// case Types.DATE:
	// case Types.TIME:
	// case Types.TIMESTAMP:
	// return ("java.util.Date");
	// default:
	// return null;
	// }
	// }

}
